﻿using FastData.Caching;
using System.Data.Linq.Mapping;
using System.Reflection;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System;
using System.Data.Common;
using System.Threading.Tasks;

namespace FastData
{
    public class DeleteQuery : QueryBase
    {
        internal DeleteQuery(DataAccess data)
            : base(data)
        {
        }

        internal override string GetCommandText(ExecuteType type)
        {
            return Data.Provider.CreateDeleteText(this, type);
        }
        
        public DeleteQuery Where(string whereText)
        {
            base.SetWhere(whereText);
            return this;
        }

        public DeleteQuery Where(string fieldName, object value)
        {
            base.SetWhere(fieldName, value);
            return this;
        }

        public DeleteQuery Where(FilterField[] filters)
        {
            base.SetWhere(filters);
            return this;
        }

        public DeleteQuery Where(string fieldName, FilterType filterType, object value)
        {
            base.SetWhere(fieldName, filterType, value);
            return this;
        }

        public DeleteQuery Param(string name, object value)
        {
            base.AddParameter(name, value);
            return this;
        }

        public DeleteQuery Param(DbParameter parameter)
        {
            base.AddParameter(parameter);
            return this;
        }

        public void ExecuteLazy()
        {
            Data.ExecuteLazy(this);
        }

        internal override void ExecuteCompleted()
        {
            CacheProvider.Remove(new TableDependency(TableName));
        }

        public int Execute()
        {
            return Data.ExecuteNonQuery(this);
        }

        public async Task<int> ExecuteAsync()
        {
            return await Data.ExecuteNonQueryAsync(this);
        }
    }

}
